<?php

namespace App\Http\Controllers\PC;

use App;
use App\Common\Status;
use App\Helper\GuzzleHttp;
use App\Helper\Helper;
use App\Http\Logic\PC\NoteLogic;
use App\Http\Controllers\Controller as BaseController;
use  App\Http\Requests\NoteRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class NoteController extends BaseController
{

    /**
     * @param NoteRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function createResume(NoteRequest $request)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $params = $request->validated();

        $res = NoteLogic::createResume($params);

        if ($res["status"] == Status::SUCCESS) {
            $r = $params['id'] ? [] : ['id' => $res["id"]];
            return $this->success(Status::SUCCESS, 'success', $r);

        } else {
            return $this->error(Status::ERROR, $res["msg"]);
        }
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function resumeDtl()
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $r = NoteLogic::getResumeDtl(Helper::pcByMobile());

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resumeUpload(Request $request)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }

        $type     = $request->post('type');
        $file     = $request->file('file');
        $fileName = $file->getClientOriginalName();

        if(strstr($fileName,'.') != "pdf") {
            return $this->error(Status::ERROR, "目前仅支持pdf文件！");
        }

        if(!$fileName) {
            return $this->error(Status::ERROR, "文件不可用！");
        }
        $time     = date('Y-m-d');
        $disk     = Storage::disk('qiniu');
        $filename = $disk->put($time, $file);//上传
        if (!$filename) {
            return $this->error(Status::ERROR, "上传失败");
        }
        # 获取下载链接
        $url = $disk->url($filename);

        if (!isset($url) || empty($url)) {
            return $this->error(Status::ERROR, "缺少资源");
        }

        # 解析出uid 里面的用户id 再获取手机号
        $id = NoteLogic::createResumeMidd(Helper::pcByMobile(), str_replace("http://", "https://", $url), $type, $fileName);

        return $this->success(Status::SUCCESS, 'success', ['r_id' => $id, 'path' => Helper::buildQiNiuUrl($url)]);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function headUpload(Request $request)
    {
        $id = $request->post("id", "");

        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $r = NoteLogic::setHeadUpload($id);

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function delResume()
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }
        $r = NoteLogic::delResumeLogic(Helper::pcByMobile());

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editResumeName(Request $request)
    {
        if (!Helper::pcByMobile()) {
            return $this->error(4001, "用户尚未绑定手机号！");
        }

        $attName = $request->input("att_name");

        $r = NoteLogic::editResumeName(Helper::pcByMobile(), $attName);

        return $this->success(Status::SUCCESS, 'success', $r);
    }


    public function resumeAnaly($id)
    {
        //    use Illuminate\Support\Facades\Storage;
        //    use Illuminate\Support\Facades\File;
        //
        //        $downloadUrl = 'http://example.com/path/to/pdf/file.pdf'; // 七牛云上的PDF文件下载链接
        //
        //        $localFilePath = storage_path('app/public/pdf/file.pdf'); // 下载到本地的文件路径
        //
        //        Storage::put($localFilePath, file_get_contents($downloadUrl));
        //    use Smalot\PdfParser\Parser;
        //
        //        $parser = new Parser();
        //        $pdf = $parser->parseFile($localFilePath);
        //
        //        // 获取PDF中的文本内容
        //        $text = $pdf->getText();
        //
        //        // 处理解析后的文本内容
        //        // ...

        //        $parser  = new \Smalot\PdfParser\Parser();
        //        $pdf     = $parser->parseFile(storage_path() . "/resume/wwww.pdf");
        //        $title   = $pdf->getDetails();
        //        $author  = $pdf->getDetails();
        //        $content = $pdf->getText();
        //
        //        var_dump($title);
        //        var_dump('-----------');
        //        var_dump('-----------');
        //
        //        var_dump($author);
        //        var_dump('-----------');
        //
        //        var_dump($content);


        # 北京无奇
        $url                    = "http://resumesdk.market.alicloudapi.com/ResumeParser";
        $appcode                = "f2b0df68b8774e2db5bea4b4ed500175";    // 替换为你的appcode
        $heard["Authorization"] = 'APPCODE ' . $appcode;
        $fname                  = "https://qiniu.eeeknow.com/Fn8a3U5y-Ut3XUV9OAWnwy276a6v?e=1713075316&token=tOdTJ-sRV_Immr3imtgWEZ0y3n8cjq65RXBeR59F:FEQyBK9CVB0n0i2WrEpdkEBBoOE=";        // 替换为你的本地文件名

        $data = array(
            'file_url'    => $fname,               // 经base64编码过的文件内容
            'file_name'   => "pdf",                // 文件名
            'need_avatar' => 0,                    // 是否需要提取头像
            'ocr_type'    => 1,                    // 1为高级ocr
        );
        $r    = GuzzleHttp::GuzzlePostNew($url, $data, $heard, 5);

        if ($r && $r["code"] == 200 && !empty($r["contents"])) {
            $info = json_decode($r["contents"], true);
            var_dump('===1111');
            var_dump($info);
        }

        var_dump('===1111');
        var_dump($r);

        //        $data   = json_encode($data, JSON_UNESCAPED_UNICODE);        // 需php5.4及以上才支持JSON_UNESCAPED_UNICODE
        //        $result = $this->http($url, $data, $appcode);
        //        var_dump($result);
        //
        //        $res_js = json_decode($result, TRUE);
        //        $name   = $res_js['result'];
        //        var_dump('===');
        //        var_dump('===');
        //        var_dump('===');
        //        var_dump($name['name']);
        //        var_dump($name);

        //        return $this->success(Status::SUCCESS, 'success', []);


        # 捕鱼科技 【一般般 解不大出来】
        //       $url = "https://www.cv-parser.com/buyu-api/parse";
        //        $username = 17321295203;
        //        $password = 123456;

        //        $fname     = "https://qiniu.eeeknow.com/Fn8a3U5y-Ut3XUV9OAWnwy276a6v?e=1713075316&token=tOdTJ-sRV_Immr3imtgWEZ0y3n8cjq65RXBeR59F:FEQyBK9CVB0n0i2WrEpdkEBBoOE=";        // 替换为你的本地文件名
        //        $fileData  = file_get_contents($fname);
        //        $content = base64_encode($fileData);
        //        var_dump('----222----');
        //        var_dump('----222----');
        //        var_dump($content);

        //
        //        $data = array(
        //            'username'   => $username,
        //            'password'   => $password,
        //            'content'   => $content,
        //            'filename'   => "pdf",
        //        );
        //
        //        $r = GuzzleHttp::GuzzlePostNew($url, $data, [], 5);
        //
        //        if ($r && $r["code"] == 200 && !empty($r["contents"])) {
        //            $info = json_decode($r["contents"],true);
        //            var_dump('===1111');
        //            var_dump('===1111');
        //            var_dump($info);
        //        }
        //        return $this->success(Status::SUCCESS, 'success', []);
    }

    public function http($url, $data, $appcode)
    {
        $process = curl_init($url);

        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8");
        curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($process, CURLOPT_HEADER, FALSE);
        curl_setopt($process, CURLOPT_TIMEOUT, 600);
        curl_setopt($process, CURLOPT_POST, TRUE);
        curl_setopt($process, CURLOPT_POSTFIELDS, $data);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
        $content = curl_exec($process);
        curl_close($process);

        return $content;
    }
}
